package cn.hellochaos.generator.service.impl;

import cn.hellochaos.generator.entity.Fishery;
import cn.hellochaos.generator.entity.dto.LogicFlagCode;
import cn.hellochaos.generator.mapper.FisheryMapper;
import cn.hellochaos.generator.service.FisheryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.hellochaos.generator.exception.bizException.BizException;

import static cn.hellochaos.generator.entity.dto.LogicFlagCode.DELETE_NOT_RESULT;

/**
* <p>
* 渔业资源相关表 服务实现类
 ** classificationId(生物学分类编号),queryId(统一编号),chineseName(中文名),englishName(英文名)
 * 要添加条件 在 addcolumn处添加，目前就是这四个
* </p>
*
* @author fmy
* @since 2021-07-20
*/
@Slf4j
@Service
public class FisheryServiceImpl extends ServiceImpl<FisheryMapper, Fishery> implements FisheryService {
    /**
     *
     * @param page 当前页数
     * @param pageSize 页的大小
     * @param queryParameter1 查询参数名
     * @param parameterValue1 查询参数值
     * @return null表示非法参数，controller来处理
     */
    @Override
    public Page<Fishery> listFisheryByPage(int page, int pageSize, String queryParameter1,String parameterValue1) {
        log.info("正在执行分页查询fishery: page = {} pageSize = {} queryParameter1 = {} parameterValue1 = {}",page,pageSize,queryParameter1,parameterValue1);
        QueryWrapper<Fishery> queryWrapper = null;
        String columnName1="";
            if(queryParameter1.equals("")&&parameterValue1.equals("")){
                return super.page(new Page<>(page, pageSize));
            }
            //add new column here
            if (queryParameter1.equals("classificationId")){
                columnName1="classification_id";
            }
            else if (queryParameter1.equals("queryId")){
                columnName1="query_id";
            }
            else if (queryParameter1.equals("chineseName")){
                columnName1="chinese_name";
            }
            else if (queryParameter1.equals("englishName")){
                columnName1="english_name";
            }

            queryWrapper =  new QueryWrapper<Fishery>().eq(columnName1, parameterValue1);

        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<Fishery> result = super.page(new Page<>(page, pageSize),queryWrapper);
        log.info("分页查询fishery完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }
    @Override
    public Fishery getFisheryById(int id) {
        log.info("正在查询fishery中id为{}的数据",id);
        Fishery fishery = super.getById(id);
        log.info("查询id为{}的fishery{}",id,(null == fishery?"无结果":"成功"));
        return fishery;
    }

    @Override
    public int insertFishery(Fishery fishery) {
        log.info("正在插入fishery");
        if (super.save(fishery)) {
            log.info("插入fishery成功,id为{}",fishery.getFisheryId());
            return fishery.getFisheryId();
        } else {
            log.error("插入fishery失败");
            throw new BizException("添加失败");
        }
    }

    @Override
    public int deleteFisheryById(int id) {
        log.info("正在删除id为{}的fishery",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的fishery成功",id);
            return id;
        } else {
            log.error("删除id为{}的fishery失败",id);
            //throw new BizException("删除失败[id=" + id + "]");
            return DELETE_NOT_RESULT;
        }
    }

    @Override
    public int updateFishery(Fishery fishery) {
        log.info("正在更新id为{}的fishery",fishery.getFisheryId());
        if(super.getById(fishery.getFisheryId())==null){
            return LogicFlagCode.UPDATE_NOT_RESULT;
        }
        if (super.updateById(fishery)) {
            log.info("更新id为{}的fishery成功",fishery.getFisheryId());
            return fishery.getFisheryId();
        } else {
            log.error("更新id为{}的fishery失败",fishery.getFisheryId());
            throw new BizException("更新失败[id=" + fishery.getFisheryId() + "]");
        }
    }

}
